package com.hjj.concurrence.research;

public class SellTickets {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        Thread t1 = new Thread(thread);
        Thread t2 = new Thread(thread);
        t1.start();
        t2.start();
    }
}

class MyThread implements Runnable {

    private int count = 100;

    @Override
    public void run() {
        while (count > 0) {
            sale();
        }
    }

    private synchronized void sale() {
        if (count > 0) {
            System.out.println(Thread.currentThread().getName() + ", 出售：" + (100 - count + 1) + "张票");
            count--;
        }
    }
}
